import { create } from "kubo-rpc-client";

import dotenv from "dotenv";
import fs from "fs";
dotenv.config("./.env");

const ipfs = create(process.env.IPFS_IRL || "http://localhost:5001");
export async function uploadFileToIPFS(filePath, file) {
  // const file = fs.readFileSync(filePath);
  console.log("file");
  console.log(file);
  const result = await ipfs.add({ path: filePath, content: file });
  return result;
}

export async function uploadJSONToIPFS(json) {
  const result = await ipfs.add(JSON.stringify(json));
  console.log(result);
  return result;
}
// 根据cid从ipfs上获取json数据
export async function getJSONFromIPFS(cid) {
  const result = ipfs.cat(cid.toString());
  // const json = JSON.parse(result);

  return result;
}
